'проточки 

ProtocMetr = 201	'метрическая
ProtocTrap = 202	'трапец.
ProtocTrub = 203	'трубная и дюймовая с угл профиля 60 гр
ProtocMetr2 = 204	'метрическая ГОСТ 27148

OUTSIDE_TYPE1 = 101	'++ Отрисовка проточки типа I (наружная резьба)
OUTSIDE_TYPE2 = 102	'++ Отрисовка проточки  типа II ( наружная резьба )
INSIDE_TYPE1 = 103	'++ Отрисовка проточки  типа I ( внутренняя резьба )	
INSIDE_TYPE2 = 104	'++ Отрисовка проточки  типа II ( внутренняя резьба )

hatchAngle = 45

BASE_LINE = 1
SLIM_LINE = 2
AXIS_LINE = 3

AXIS_END=3
AXIS_END1=1
OVERMESURE=5  'припуск

Kx=1'положение верх/низ
Ky=1'положение право/лево

'###############################################	закомментить в релизе - начало
REM FlagTrub =1 '2=коническая дюймовая\1=трубная коническая\0=трубная цилиндрич\

REM TYPE_Protoc = ProtocMetr

REM TYPE_LEVEL = INSIDE_TYPE1

REM 'GCV_EXTENDED = 1
REM 'GCV_SIMPLIFIED
REM DETAIL_LEVEL = GCV_EXTENDED

REM DETAIL_LEVEL=3
REM Delta_d=0.5
REM FlagTrub=0
REM P=1
REM TYPE_LEVEL=104
REM TYPE_Protoc=201
REM b_norm=3.6
REM d=6
REM hatchStep=2
REM r1_norm=0
REM r_norm=2
REM g2 = 7.8

REM 'метрическая ГОСТ 27148 внутренняя
REM DETAIL_LEVEL = GCV_EXTENDED
REM Delta_d=0.5
REM FlagTrub=0
REM P=1.5
REM TYPE_LEVEL=103
REM TYPE_Protoc=204
REM b_norm=6
REM d=10
REM hatchStep=2
REM r1_norm=0.8
REM r_norm=0.8
REM g2 = 7.8

'метрическая ГОСТ 27148 наружная
REM DETAIL_LEVEL = GCV_EXTENDED
REM Delta_d=2.3
REM FlagTrub=0
REM P=1.5
REM TYPE_LEVEL=101
REM TYPE_Protoc=204
REM b_norm=2.5
REM d=10
REM hatchStep=1
REM r1_norm=0.8
REM r_norm=0.8
REM g2 = 4.5

'###############################################	закомментить в релизе - конец 

if DETAIL_LEVEL = GCV_EXTENDED then
	Dims = true
else
	Dims = false
end if

Dim math, iDA, iPoint, con, con1, cn, tlarr, tl, tiarr, ti, vp, iter, ltvar, dimText, lDimSource, dimDrawing, aDimSource, lDimParam, aDimParam, rDimDrawing, rDimSource, rDimParam
Dim x0, y0
dim ad, dt
Const PI = 3.1415926535897931

Const ksDocumentFragment = 2

Const POINT_ARR = 2  
Const TEXT_LINE_ARR = 3
Const TEXT_ITEM_ARR = 4
Const ALLPARAM = -1
Const VIEW_LAYER_STATE = 1
Const VIEW_OBJ = 123

Const ko_ViewParam = 8
Const ko_MathPointParam = 14
Const ko_TextLineParam = 29
Const ko_TextItemParam = 31
Const ko_CON = 41
Const ko_DimText = 42
Const ko_LDimSourceParam = 43
Const ko_DimDrawing = 44
Const ko_LDimParam = 45
Const ko_ADimSourceParam = 49
Const ko_ADimParam = 50
Const ko_RDimSourceParam = 52
Const ko_RDimDrawing = 53
Const ko_RDimParam = 54
Const ko_LtVariant = 108

'++++

SUB LINE (x1,y1,x2,y2,t) Doc.ksLineSEG x1,y1,x2,y2,t END SUB

SUB LinDimension (x1, y1, x2, y2, dx, dy, n1sh, n2sh, n1arr, n2arr, sign, text, textBase, basePoint)

	dimText.Init true
	dimDrawing.Init
	lDimSource.Init
	ti.Init
	tl.Init

	'-----------------------
	lDimSource.x1 = x1
	lDimSource.y1 = y1

	lDimSource.x2 = x2
	lDimSource.y2 = y2
	
	lDimSource.basePoint = basePoint
	lDimSource.ps = 3
	
	lDimSource.dx = dx
	lDimSource.dy = dy
	
	'------------------------
	dimDrawing.pt1 = n1arr
	dimDrawing.pt2 = n2arr
	
	dimDrawing.pl1 = n1sh
	dimDrawing.pl2 = n2sh
	
	dimDrawing.lenght = 0
	dimDrawing.shelfDir = 0
	dimDrawing.textBase = textBase
	
	'------------------------
	IF (text <> 0) THEN
		tiarr.ksClearArray
		tlarr.ksClearArray
		ti.s = text
		tiarr.ksAddArrayItem -1, ti
		tl.SetTextItemArr tiarr
		tl.style = 1
		tlarr.ksAddArrayItem -1, tl


		dimText.SetTextArr tlarr
		dimText.bitFlag = 0
	ELSE
		dimText.bitFlag = 1
	END IF
	dimText.sign = sign
	
	'-------------------------
	lDimParam.SetTPar dimText
	lDimParam.SetDPar dimDrawing
	lDimParam.SetSPar lDimSource

	doc.ksLinDimension lDimParam

END SUB

SUB AngDimension (xc, yc, x1, y1, x2, y2, ang1, ang2, rad, nLen, nAng)

	dimText.Init true
	dimDrawing.Init
	aDimSource.Init

	'-----------------------
	aDimSource.xc = xc
	aDimSource.yc = yc
	
	aDimSource.x1 = x1
	aDimSource.y1 = y1

	aDimSource.x2 = x2
	aDimSource.y2 = y2
	
	aDimSource.ang1 = ang1
	aDimSource.ang2 = ang2
	
	aDimSource.rad = rad
	'------------------------
	arcL = rad * pi * abs(ang1 - ang2) / 180
		
	if (arcL > 10) then
		ks = 1
	else
		ks = 2
	end if
		
	dimDrawing.pt1 = ks
	dimDrawing.pt2 = ks
	
	dimDrawing.lenght = nLen
	dimDrawing.ang = nAng
	dimDrawing.shelfDir = 1
	
	'------------------------
	dimText.bitFlag = 1
	
	'-------------------------
	aDimParam.SetTPar dimText
	aDimParam.SetDPar dimDrawing
	aDimParam.SetSPar aDimSource

	doc.ksAngDimension aDimParam

END SUB

SUB RadDimension (xc, yc, rad, textPos, nAng, sd, n1arr, n2arr)

	dimText.Init true
	rDimDrawing.Init
	rDimSource.Init

	'-----------------------
	rDimSource.xc = xc
	rDimSource.yc = yc
	
	rDimSource.rad = rad
	'------------------------
	rDimDrawing.pt1 = n1arr
	rDimDrawing.pt2 = n2arr

	rDimDrawing.textPos = textPos
	rDimDrawing.ang = nAng

	rDimDrawing.shelfDir = sd

	'------------------------
	dimText.bitFlag = 1
	dimText.sign = 3
	
	'-------------------------
	rDimParam.SetTPar dimText
	rDimParam.SetDPar rDimDrawing
	rDimParam.SetSPar rDimSource

	doc.ksRadDimension rDimParam

END SUB

' Функция для вычисления значения масштабирования
dim getScaleField

getScaleField = 0

function GetScale ()
	if getScaleField = 0 then
		ef = false
		i = 0
		vp.Init
		ltvar.Init
		documentType = kompas.ksGetDocumentType(0)
		IF documentType <> 3 THEN 'фрагмент
			iter.ksCreateIterator VIEW_OBJ, 0
			ref = iter.ksMoveIterator ("F")
			IF ref <> 0 THEN 
				DO
					i = i + 1
					doc.ksGetObjParam ref, vp, ALLPARAM
					IF vp.state = 3 THEN 'текущий вид
						getScaleField = vp.scale_
						ef = true
					END IF
					ref = iter.ksMoveIterator ("N")
				LOOP WHILE (ref <> 0 and ef = false and i < 1000)
			END IF
		END IF
		IF getScaleField = 0 THEN getScaleField = 1
	end if
	getScale = getScaleField
end function

' Возвращает тип стрелки для линейных размеров по размеру
function GetArrowType (value)
	
	scale = GetScale
	if abs(value * scale) < 10 then
		GetArrowType = 2
	else
		GetArrowType = 1
	end if
	
end function
'+++++


Sub GetIntersectLinSLin(LineSX1, LineSY1, LineSX2, LineSY2, LineX1, LineY1, LineAngle, PointX, PointY)
	PointX = 0
	PointY = 0
	Math.ksIntersectLinSLine LineSX1, LineSY1, LineSX2, LineSY2, LineX1, LineY1, LineAngle, iDA
	If iDA.ksGetArrayCount = 1 then
		iDA.ksGetArrayItem 0, iPoint
		PointX = iPoint.x
		PointY = iPoint.y
		iDa.ksClearArray
	End If
End Sub

'++++

SUB Script_OnLoad ()

	Set math = Kompas.GetMathematic2D
	Set iDA = Kompas.GetDynamicArray(POINT_ARR)
	Set tlarr = Kompas.GetDynamicArray(TEXT_LINE_ARR)
	Set tiarr = Kompas.GetDynamicArray(TEXT_ITEM_ARR)
	Set iter = Kompas.GetIterator

	Set con = Kompas.GetParamStruct(ko_CON)
	Set con1 = Kompas.GetParamStruct(ko_CON)
	Set cn = Kompas.GetParamStruct(ko_CON)
	Set iPoint = Kompas.GetParamStruct(ko_MathPointParam)
	set tl = Kompas.GetParamStruct(ko_TextLineParam)
	Set ti = Kompas.GetParamStruct(ko_TextItemParam)
	Set dimText = Kompas.GetParamStruct(ko_DimText)
	Set lDimSource = Kompas.GetParamStruct(ko_LDimSourceParam)
	Set dimDrawing = Kompas.GetParamStruct(ko_DimDrawing)
	Set lDimParam = Kompas.GetParamStruct(ko_LDimParam)
	Set aDimSource = Kompas.GetParamStruct(ko_ADimSourceParam)
	Set aDimParam = Kompas.GetParamStruct(ko_ADimParam)
	Set rDimDrawing = Kompas.GetParamStruct(ko_RDimDrawing)
	Set rDimSource = Kompas.GetParamStruct(ko_RDimSourceParam)
	Set rDimParam = Kompas.GetParamStruct(ko_RDimParam)
	Set vp = Kompas.GetParamStruct(ko_ViewParam)
	Set ltvar = Kompas.GetParamStruct(ko_LtVariant)

	set ad = komapp.ActiveDocument

	' Init
	x0 = 0
	y0 = 0
	  
	SELECT CASE TYPE_Protoc
	
	CASE ProtocMetr:
		'метрическая
		PAR_d_p = p * 0.541265877
		PAR_d_3 = delta_d * 0.5
		SELECT CASE TYPE_LEVEL
		
		CASE OUTSIDE_TYPE1:
			CalcProtoc1 PAR_D_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1 
			DrawProtoc1 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr    

		CASE OUTSIDE_TYPE2:
			CalcProtoc2 PAR_D_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1 
			DrawProtoc2 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr
        
		CASE INSIDE_TYPE1:
			CalcProtocSec1 PAR_d_p, PAR_D_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1 
			DrawProtocSec1 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr
        
		CASE INSIDE_TYPE2:
			'CalcProtocSec2 d_p, d_3, kx, ky, x0, y0
			'Doc.ksMtr -x0, -y0, 0, 1, 1
			DrawProtocSec2 PAR_D_P, PAR_D_3, Kx, Ky
			'Doc.KsDeleteMtr
			
		END SELECT
		
	CASE ProtocTrap:
		'трапец.
		PAR_d_p = p * 0.5
		PAR_d_3 = delta_d * 0.5
		SELECT CASE  TYPE_LEVEL 
		
		CASE INSIDE_TYPE1 :
			CalcProtocSec1 PAR_d_p, PAR_d_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1
			DrawProtocSec1 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr

		CASE OUTSIDE_TYPE1 :
			CalcProtoc1 PAR_d_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1 
			DrawProtoc1 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr
			
		END SELECT       

	CASE ProtocTrub:
		'трубная
		If (FlagTrub = 1) or (FlagTrub = 0) Then
			PAR_d_p = p * 0.640327
		else
			PAR_d_p = p * 0.8
		End If
		PAR_d_3 = 0.5 * Abs(delta_d - d)

		SELECT CASE TYPE_LEVEL
		
		CASE INSIDE_TYPE1:
			CalcProtocSec1 PAR_d_p, PAR_d_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1
			DrawProtocSec1 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr

		CASE OUTSIDE_TYPE1:
			CalcProtoc1 PAR_d_3, kx, ky, x0, y0
			Doc.ksMtr -x0, -y0, 0, 1, 1 
			DrawProtoc1 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr
			
		END SELECT             

	CASE ProtocMetr2:
		'метрическая ГОСТ 27148
		PAR_d_p = p * 0.541265877
		PAR_d_3 = delta_d * 0.5
		SELECT CASE TYPE_LEVEL
		
		CASE OUTSIDE_TYPE1:
			ang1 = Math.ksAngle (0, 0, - (g2 - b_norm) * kx, - PAR_d_3 * ky)

			if ang1 < (180 + 30) then 'потому что минимальный угол 30
				g2 = b_norm + (PAR_d_3 / tan((30*PI)/180))
			end if

			x0 = - g2 * kx
			y0 = (- PAR_d_3 + r_norm) * ky
			Doc.ksMtr - x0, - y0, 0, 1, 1 
			DrawProtocMetr2 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr    

		CASE INSIDE_TYPE1:
			x0 = - g2 * kx
			y0 = (PAR_d_3 + PAR_d_p - r_norm)*ky
			Doc.ksMtr - x0, - y0, 0, 1, 1 
			DrawProtocSecMetr2 PAR_D_P, PAR_D_3, Kx, Ky
			Doc.KsDeleteMtr
        
		END SELECT

	END SELECT

	IDA.ksDeleteArray 
END SUB  
'+++++++++++


'---------------------------------------------------------------------------
' Рассчитать точку привязки проточки относительно торца вала
'---------------------------------------------------------------------------
Sub CalcProtoc1 (d_3,kx,ky,byref x0,byref y0)
	x0 = (-b_norm-d_3)*kx
	y0 = (-d_3+r_norm)*ky
End Sub


'---------------------------------------------------------------------------
' Отрисовка проточки типа 1 (наружная резьба) (трубная коническая)
'---------------------------------------------------------------------------
SUB DrawProtoc1 (d_p, d_3, kx, ky) 

	IDA.ksClearArray
	math.ksIntersectLinLin 0, 0, 45 * ky, 0, - d_p * ky, 0, iDA 
	ks = iDA.ksGetArrayCount
	
	If ks<>0 Then
	
		iDA.ksGetArrayItem 0, iPoint
	    xp = iPoint.x
	    yp = iPoint.y
		
	End If
	
	ang1 = Math.ksAngle (0,0,-10*kx,-10*ky)
	ang2 = Math.ksAngle (0,-d_3*ky, -b_norm*kx, -d_3*ky)
	
	math.ksCouplingLineLine 0,0,ang1, 0,-d_3*ky,ang2, r1_norm ,cn
	ks = Cn.GetCount
	
	if (ky=1) then
		if kx =1 then       
			index=3
		else 
			index=2
		end if
	else
		if kx =1 then       
			index=2
		else 
			index=3
		end if
	end if 

	xc=cn.GETxc(index)
	yc=cn.GETyc(index)

	x1=cn.GETx1(index)
	y1=cn.GETy1(index)
	
	x2=cn.GETx2(index)
	y2=cn.GETy2(index)
	
	Xc1=(-b_norm-d_3+r_norm)*kx
	Yc1=(-d_3+r_norm)*ky
	x11=xc1
	y11=-d_3*ky
	x21=(-b_norm-d_3)*kx
	y21=yc1

	doc.ksColouring &HFE000000
		Line 1.5*b_norm*kx,0,0,0,1
		Line 0,0,x1,y1,1
		Doc.ksArcByPoint xc, yc,r1_norm,x1,y1,x2,y2,-ky*kx,1
		Line x2,y2,x11,y11,1
		Doc.ksArcByPoint xc1,yc1,r_norm,x11,y11,x21,y21,-ky*kx, 1
		Line x21,y21+b_norm*0.3,x21,-1.3*b_norm*ky,1
		Doc.ksBezier 0,0     
			Doc.kspoint  x21, y21 + b_norm * 0.3, 11
			Doc.kspoint  -1.8*b_norm*kx,-b_norm*0.3*ky ,11
			Doc.kspoint  -1.8*b_norm*kx,-b_norm*0.8*ky ,11
			Doc.kspoint  (-b_norm-d_3)*kx,-1.3*b_norm*ky ,11
			Doc.kspoint  -d_3*kx,(-d_3-0.9*b_norm)*ky ,11
			Doc.kspoint  0,-1.2*b_norm*ky ,11
			Doc.kspoint  1.5*b_norm*kx,-d_p*ky ,11
			Doc.kspoint  1.5*b_norm*kx,0 ,11
		Doc.ksEndObj
	Doc.ksEndObj

	Line 1.5*b_norm*kx,0,0,0,1

	line 1.5*b_norm*kx,-d_p*ky, xp*kx, -d_p*ky, 2
	line 0,0, 0, -1.2*b_norm*ky, 1
	line -d_3*kx, (-d_3-b_norm*0.1)*ky, -d_3*kx, (-d_3-0.9*b_norm)*ky, 2

	line 0,0,x1,y1,1

	Doc.ksArcByPoint xc, yc,r1_norm,x1,y1,x2,y2,-ky*kx,1
	
	line x2,y2,x11,y11,1
	Doc.ksArcByPoint xc1,yc1,r_norm,x11,y11,x21,y21,-ky*kx, 1

	Line x21,y21+b_norm*0.3,x21,-1.3*b_norm*ky,1

	IF (Dims = true) THEN
	
		scale = getScale
		
		ks = GetArrowType(x21 + d3 * kx)

		LinDimension x21, - 1.3 * b_norm * ky, - d_3 * kx, (- d_3 - 0.9 * b_norm) * ky, 0, - 30, false, false, ks, ks, 0, 0, 0, 1
	 
	 
		IF r1_norm = r_norm THEN
		
			b = - 25
			a = - abs(xc1 - xc) * 0.5
			alfa = atn(b / a) * 180 / pi
			c = b / sin(alfa * pi / 180)
			xx = c * 0.9

			iDA.ksClearArray
			iPoint.init
			iPoint.x = xc1
			iPoint.y = yc1
			iDA.ksAddArrayItem -1, iPoint
			iPoint.init
			iPoint.x = xc
			iPoint.y = yc
			iDA.ksAddArrayItem -1, iPoint

			' #VBG
			VBGuru.BranchingRadialDimension kompas, iDA, r_norm, xx, -(alfa * kx) * ky, -1, 0, x0, y0, scale, 0, 0
			
		ELSE
		
			b = - 15 - 1 * scale
			a = -abs(xc1 - xc) * 0.5
			alfa = atn(b / a) * 180 / pi
			c = b / sin(alfa * pi / 180)
			xx = c * 0.9

			RadDimension xc, yc, r1_norm, xx, -(alfa * kx) * ky, 1, 1, 2
			RadDimension xc1, yc1, r_norm, xx, -(180 - alfa * kx) * ky, -1, 1, 2
			
		END IF
		
		IF kx=1 THEN
		
			ang1 = 0
			ang2 = 45
			nAng = 55
			
		ELSE
		
			ang1 = 180
			ang2 = 180 - 45
			nAng = 180 - 55
			
		END IF
		
		AngDimension 0, 0, 1.5 * b_norm * kx, 0, 0, 0,  ang1, ang2 * ky, 15, 8, nAng
	
		xx1 = - 2 * b_norm * ky
		
		IF abs(xx1) < 18 / scale THEN xx1 = (- 18 * ky) / scale
		
		LinDimension x11, y2, 0, xx1, - 10 - (1.8 * b_norm) * scale, 0, false, true, 1, 0, 1, d - 2 * d_3, 0, 2
		
	END IF

    Doc.ksBezier 0,0     
		Doc.kspoint  x21,y21+b_norm*0.3 ,11
		Doc.kspoint  -1.8*b_norm*kx,-b_norm*0.3*ky ,11
		Doc.kspoint  -1.8*b_norm*kx,-b_norm*0.8*ky ,11
		Doc.kspoint  (-b_norm-d_3)*kx,-1.3*b_norm*ky ,11
		Doc.kspoint  -d_3*kx,(-d_3-0.9*b_norm)*ky ,11
		Doc.kspoint  0,-1.2*b_norm*ky ,11
		Doc.kspoint  1.5*b_norm*kx,-d_p*ky ,11
		Doc.kspoint  1.5*b_norm*kx,0 ,11
	Doc.ksEndObj
END SUB


'---------------------------------------------------------------------------
' Рассчитать точку привязки проточки относительно торца вала
'---------------------------------------------------------------------------
Sub CalcProtoc2 (d_3,kx,ky,byref x0,byref y0)   
	r_copy=r_norm '*-0.1*
	ks=0
	ang1 = Math.ksAngle (0,0,-10*kx,-10*ky)
	ang2 = Math.ksAngle (-b_norm*kx,-10*ky,-b_norm*kx,10*ky)

	math.ksCouplingLineLine 0,0,ang1,-b_norm*kx,-10*ky,ang2, r_copy ,cn
	ks = Cn.GetCount

	if (ky=1) then
		if kx =1 then       
			index=3
		else 
			index=2
		end if
	else
		if kx =1 then       
			index=2
		else 
			index=3
		end if
	end if 

	xc=cn.GETxc(index)
	yc=cn.GETyc(index)
	
	x1=cn.GETx1(index)
	y1=cn.GETy1(index)

	x2=cn.GETx2(index)
	y2=cn.GETy2(index)	 

	if (x1*kx<x1*kx ) Then
		x0 = x1
		y0 = y1
	else 
		x0 =x2
		y0 =y2
	End If
End Sub	


'---------------------------------------------------------------------------
' Отрисовка проточки  типа II ( наружная резьба )
'---------------------------------------------------------------------------
SUB DrawProtoc2 (d_p, d_3, kx, ky) 
	Dim xp(2),yp(2)
	Dim xt(2),yt(2)
	Dim xp1(2),yp1(2)

	r_copy=r_norm    '-0.1

	ang1 = Math.ksAngle (0,0,-10*kx,-10*ky)
	ang2 = Math.ksAngle (-b_norm*kx,-10*ky,-b_norm*kx,10*ky)

	math.ksCouplingLineLine 0,0,ang1,-b_norm*kx,-10*ky,ang2, r_copy ,cn
	ks = Cn.GetCount

	if (ky=1) then
		if kx =1 then       
			index=3
		else 
			index=2
		end if
	else
		if kx =1 then       
			index=2
		else 
			index=3
		end if
	end if 

	xc=cn.GETxc(index)
	yc=cn.GETyc(index)

	x1=cn.GETx1(index)
	y1=cn.GETy1(index)

	x2=cn.GETx2(index)
	y2=cn.GETy2(index)	

	if ( x1*kx < x1*kx ) then
		xp(0) = x1
		yp(0) = y1
	else 
		xp(0) = x2
		yp(0) = y2
	end if

	IDA.ksClearArray
	Math.ksTanLineAngCircle xc, yc, r_copy, 0, iDA
	ks = iDA.ksGetArrayCount

	If ks<>0 Then 
		iDA.ksGetArrayItem 0, iPoint
		xt(0) = iPoint.x
		yt(0) = iPoint.y
		
		If ks>1 Then     
			iDA.ksGetArrayItem 1, iPoint
			xt(1) = iPoint.x
			yt(1) = iPoint.y
		End If   
	End If  

	IDA.ksClearArray
	f1=Math.ksAngle (xc,yc,x1,y1)
	f2=Math.ksAngle (xc,yc,xp(0),yp(0))
	f3=Math.ksAngle (1.5*b_norm*kx,-d_p*ky,xc-100*kx, -d_p*ky)

	Math.ksIntersectArcLin xc,yc,r_copy,f1,f2,-kx*ky,1.5*b_norm*kx,-d_p*ky,f3,iDA
	kp = iDA.ksGetArrayCount
	If kp <>0 Then
		iDA.ksGetArrayItem 0, iPoint
		xp1(0) = iPoint.x
		yp1(0) = iPoint.y
		
		If kp>1 Then
			iDA.ksGetArrayItem 1, iPoint
			xp1(1) = iPoint.x
			yp1(1) = iPoint.y
		End If
	End If
	
	if( kp = 2 ) then
		if( xp1(0)*kx<xp1(1)*kx) then
			yp1(0) = xp1(1)
		else
			yp1(0) = xp1(0)
		end if 
	else 
		if ( kp=1 ) then
			yp1(0) = xp1(0)
		else
			yp1(0) =0
		end if
	end if

	Doc.ksColouring &HFE000000
		line 1.5*b_norm*kx,0,0,0,1
		line 0,0,x1,y1,1
		Doc.ksArcByPoint xc,yc, r_copy,x1,y1,xp(0),yp(0),-ky*kx,1
		line -b_norm*kx,yp(0),-b_norm*kx,yp(0)+b_norm*0.3,1
		Doc.ksBezier 0,0     
			Doc.kspoint -b_norm*kx,yp(0)+b_norm*0.3,11  ' yp(0)=tmp.b_norm*0.2*ky
			Doc.kspoint -1.5*b_norm*kx,-b_norm*0.3*ky,11
			Doc.kspoint -1.5*b_norm*kx,-b_norm*0.8*ky,11
			Doc.kspoint -b_norm*kx,-b_norm*ky,11
			Doc.kspoint 0,-b_norm*ky,11
			Doc.kspoint 1.5*b_norm*kx,-d_p*ky,11
			Doc.kspoint 1.5*b_norm*kx,0,11
		Doc.ksEndObj
	Doc.ksEndObj

	line 1.5*b_norm*kx,0,0,0,1
	line 0,0, 0, -b_norm*ky, 1
	line 0,0,x1,y1,1
	Doc.ksArcByPoint xc,yc, r_copy,x1,y1,xp(0),yp(0),-ky*kx,1
	line 1.5*b_norm*kx,-d_p*ky, yp1(0), -d_p*ky, 2
	''''yp(0)=b_norm*0.2*ky
	line -b_norm*kx,yp(0)+b_norm*0.3,-b_norm*kx,-b_norm*ky,1

	IF (Dims = true) THEN
	
		scale = getScale
			
		ks = GetArrowType(b_norm)
		LinDimension - b_norm * kx, - b_norm * ky, 0, - b_norm * ky, 0, - 30 * ky, false, false, ks, ks, 0, 0, 0, 1
	 
		xx = - 27
		
		RadDimension xc, yc, r_norm, xx, - (90 + 15 * kx) * ky, - 1, 1, 2
		
		IF kx=1 THEN
			ang1 = 0
			ang2 = 45
			nAng = 55
		ELSE
			ang1 = 180
			ang2 = 180 - 45
			nAng = 180 - 55
		END IF
		
		AngDimension 0, 0, 1.5 * b_norm * kx, 0, 0, 0,  ang1, ang2 * ky, 15, 8, nAng
	
		xx = -1.4 * b_norm - 0.05
		
		IF xx > -12/scale THEN xx = -12/scale
		
		xx1 = -2 * b_norm * ky
		
		IF abs(xx1) < 18/scale THEN xx1 = (-18 * ky)/scale
		
		LinDimension xc, yc - r_norm, xc, xx1, xx * kx * scale, 0, false, true, 1, 0, 1, d-2*d_3, 0, 1
		
	END IF
	
	'линии обрыва   
	Doc.ksBezier 0,0     
		Doc.kspoint -b_norm*kx,yp(0)+b_norm*0.3,11  ' yp(0)=tmp.b_norm*0.2*ky
		Doc.kspoint -1.5*b_norm*kx,-b_norm*0.3*ky,11
		Doc.kspoint -1.5*b_norm*kx,-b_norm*0.8*ky,11
		Doc.kspoint -b_norm*kx,-b_norm*ky,11
		Doc.kspoint 0,-b_norm*ky,11
		Doc.kspoint 1.5*b_norm*kx,-d_p*ky,11
		Doc.kspoint 1.5*b_norm*kx,0,11
	Doc.ksEndObj
END Sub


'---------------------------------------------------------------------------
' Рассчитать точку привязки проточки относительно торца вала
'---------------------------------------------------------------------------
Sub CalcProtocSec1 (d_p, d_3, kx, ky, byref x0, byref y0) 
	x0 = (-b_norm-d_3-d_p)*kx
	y0 = (d_3+d_p-r_norm)*ky
End Sub


'---------------------------------------------------------------------------
' Отрисовка проточки  типа I ( внутренняя резьба )
'---------------------------------------------------------------------------
SUB DrawProtocSec1(d_p, d_3, kx, ky)

	ang1 = Math.ksAngle (0, 0, - 10 * kx, 10 * ky)
	ang2 = Math.ksAngle (0, (d_3 + d_p) * ky, - b_norm * kx, (d_3 + d_p) * ky)

	math.ksCouplingLineLine 0, 0, ang1, 0, (d_3 + d_p) * ky, ang2, r1_norm, cn
	ks = Cn.GetCount

	if (ky = - 1) then
		if kx = 1 then       
			index = 3
		else 
			index = 2
		end if
	else
		if kx = 1 then       
			index = 2
		else 
			index = 3
		end if
	end if 

	xc=cn.GETxc(index)
	yc=cn.GETyc(index)

	x1=cn.GETx1(index)
	y1=cn.GETy1(index)

	x2=cn.GETx2(index)
	y2=cn.GETy2(index)

	xc1=(-b_norm-d_3-d_p+r_norm)*kx
	yc1=(d_3+d_p-r_norm)*ky
	x11=xc1 
	y11=(d_3+d_p)*ky
	x21=(-b_norm-d_3-d_p)*kx
	y21=yc1

	Doc.ksColouring &HFE000000
		Doc.ksBezier 0,0     
			Doc.kspoint(-b_norm-d_3-d_p)*kx,y21-b_norm*0.3,11
			Doc.kspoint(-b_norm-d_3-d_p)*kx-b_norm*0.1,y21,11

			Doc.kspoint(-b_norm-d_3-d_p-1)*kx, 1.1*b_norm*ky,11
			Doc.kspoint(-d_3-d_p)*kx,1.1*b_norm*ky,11
			Doc.kspoint 0,1.1*b_norm*ky,11
			Doc.kspoint 1.5*b_norm*kx,d_p*ky,11
			Doc.kspoint 1.5*b_norm*kx,0,11
		Doc.ksEndObj

		Doc.ksBezier 0,0     
			Doc.kspoint 1.5*b_norm*kx,0,11
			Doc.kspoint 0,-b_norm*1.1*ky,11
			Doc.kspoint x21+b_norm*kx , -b_norm*1.1*ky,11 
			Doc.kspoint(-b_norm-d_3-d_p)*kx,y21-b_norm*0.3,11
		Doc.ksEndObj
	Doc.ksEndObj

	line 1.5*b_norm*kx,0,0,0,1
	line 1.5*b_norm*kx,d_p*ky, -d_p*kx, d_p*ky, 2			
	line 0,0,x1,y1,1

	Doc.ksArcByPoint xc,yc,r1_norm,x1,y1,x2,y2,ky*kx,1

	line x2,y2,x11,y11,1
	Doc.ksArcByPoint xc1,yc1,r_norm,x11,y11,x21,y21,ky*kx,1
	
	line x21,y21,x21,y21-b_norm*0.3,1
	'++линии  обрыва++
	Doc.ksBezier 0,0
		Doc.kspoint(-b_norm-d_3-d_p)*kx,y21-b_norm*0.3,11
		Doc.kspoint(-b_norm-d_3-d_p)*kx-b_norm*0.1,y21,11
		
		Doc.kspoint(-b_norm-d_3-d_p-1)*kx, 1.1*b_norm*ky,11
		Doc.kspoint(-d_3-d_p)*kx,1.1*b_norm*ky,11
		Doc.kspoint 0,1.1*b_norm*ky,11
		Doc.kspoint 1.5*b_norm*kx,d_p*ky,11
		Doc.kspoint 1.5*b_norm*kx,0,11
	Doc.ksEndObj

	Doc.kshatch 0, hatchAngle, hatchStep, 0, 1, 0
		line 1.5*b_norm*kx,0,0,0,1
		line 0,0,x1,y1,1
		
		Doc.ksArcByPoint xc,yc,r1_norm,x1,y1,x2,y2,ky*kx, 1 
		
		line x2,y2,x11,y11,1
		Doc.ksArcByPoint xc1,yc1,r_norm,x11,y11,x21,y21,ky*kx, 1 
		
		line x21,y21,x21,y21-b_norm*0.3,1
		'++линии  обрыва++   
		Doc.ksBezier 0, 0
			Doc.kspoint (- b_norm - d_3 - d_p) * kx, y21 - b_norm * 0.3, 11
			Doc.kspoint (- b_norm - d_3 - d_p) * kx - b_norm * 0.1, y21, 11
			
			Doc.kspoint (- b_norm - d_3 - d_p - 1) * kx, 1.1 * b_norm * ky, 11 
			Doc.kspoint (- d_3 - d_p) * kx, 1.1 * b_norm * ky, 11
			Doc.kspoint 0, 1.1 * b_norm * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, d_p * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, 0, 11
		Doc.ksEndObj
	Doc.ksEndObj
	line 0, 0, 0, -b_norm * 1.1 * ky, 1

	IF (Dims = true) THEN
	
		scale = getScale
		
		ks = GetArrowType(- b_norm - d_3 - d_p - (x21 + b_norm))
		LinDimension (- b_norm - d_3 - d_p) * kx, y21, x21 + b_norm * kx, y2 - d_p * 0.5 * ky, 0, (1.1 * b_norm * scale + 5) * ky, false, false, ks, ks, 0, 0, 0, 2
	 
	 
 		IF r1_norm = r_norm THEN
		
			xx1 = - b_norm * 1.5 * scale - 10
			xx = ((-abs(xc1 - xc) * 0.5 / sin(pi * 5 / 180))) * scale
			if (xx < xx1) THEN xx = xx1

			iDA.ksClearArray
			iPoint.init
			iPoint.x = xc1
			iPoint.y = yc1
			iDA.ksAddArrayItem -1, iPoint
			iPoint.init
			iPoint.x = xc
			iPoint.y = yc
			iDA.ksAddArrayItem -1, iPoint

			' #VBG
			VBGuru.BranchingRadialDimension kompas, iDA, r_norm, xx, (90 + 5 * kx) * ky, 1, 0, x0, y0, scale, 0, 0
			
		ELSE
		
			b = - b_norm * 1.7 * scale - 10
			if b > - 25 then b = - 25
			a = -abs(xc1 - xc) * 0.5
			alfa = atn(b / a) * 180 / pi
			c = b / sin(alfa * pi / 180)
			xx = c * 0.9

			RadDimension xc, yc, r1_norm, xx, ( alfa * kx) * ky, 1, 1, 2
			RadDimension xc1, yc1, r_norm, xx, (180 - alfa * kx) * ky, -1, 1, 2
			
		END IF
		
		IF kx = 1 THEN
		
			ang1 = -45
			ang2 = 0
			nAng = -55
			
		ELSE
		
			ang1 = 180 + 45
			ang2 = 180
			nAng = 180 + 55
			
		END IF
		
		AngDimension 0, 0, 1.5 * b_norm * kx, 0, 0, 0,  ang1, ang2 * ky, 5 + b_norm * 1.5 * scale, 5, nAng
	
		xx1 = -1.3 * b_norm_tmp * ky
		
		IF abs(xx1) < 18/scale THEN xx1 = (-18 * ky)/scale
		
		LinDimension x11, y2, x11, xx1, - 10 + (- b_norm - d_3 - d_p - 1 - x11) * kx * scale, 0, false, true, 1, 0, 1, d + d_3 * 2, 0, 1
	END IF  

	Doc.ksBezier 0,0     
		Doc.kspoint 1.5 * b_norm * kx, 0, 11
		Doc.kspoint 0, - b_norm * 1.1 * ky, 11
		Doc.kspoint x21 + b_norm * kx, - b_norm * 1.1 * ky, 11 
		Doc.kspoint (- b_norm - d_3 - d_p) * kx, y21 - b_norm * 0.3, 11
	Doc.ksEndObj

	line x21 + b_norm * kx, -b_norm * 1.1 * ky, x21 + b_norm * kx, y2 - d_p * 0.5 * ky, 2
END Sub


'---------------------------------------------------------------------------
' Рассчитать точку привязки проточки относительно торца вала
'---------------------------------------------------------------------------
Sub CalcProtocSec2 (d_p, d_3, kx, ky, byref x0, byref y0)

	r_copy=r_norm

	ks=0

	while ks=0 

		ang1 = Math.ksAngle (0,0,-10*kx,10*ky)
		ang2 = Math.ksAngle (0,(d_3+d_p)*ky, -b_norm*kx, (d_3+d_p)*ky)

		math.ksCouplingLineLine 0,0,ang1,0,(d_3+d_p)*ky,ang2,r_copy,cn
		ks = Cn.GetCount	

		if (ky=-1) then
			if kx =1 then       
				index=3
			else 
				index=2
			end if
		else
			if kx =1 then       
				index=2
			else 
				index=3
			end if
		end if 

		xc=cn.GETxc(index)
		yc=cn.GETyc(index)

		x1=cn.GETx1(index)
		y1=cn.GETy1(index)

		x2=cn.GETx2(index)
		y2=cn.GETy2(index)

		IDA.ksClearArray
		math.ksIntersectLinSCir -b_norm*kx,10*ky,-b_norm*kx,-10*ky,xc,yc,r_copy,iDA
		ks = iDA.ksGetArrayCount
		
		If ks<>0 Then   
			iDA.ksGetArrayItem 0, iPoint
			xx1 = iPoint.x
			yy1 = iPoint.y
			
			If ks >1 Then 
				iDA.ksGetArrayItem 1, iPoint
				xx2 = iPoint.x
				yy2 = iPoint.y
			End If 
		End IF
		
		if ks=0 then
			r_copy=r_copy+0.1
		end if
	Wend ' end while

	if  ((ks>1) and (yy2*ky > yy1*ky )) then
		yy1 = yy2
	end if  

	x0 = xx1
	y0 = yy1
End Sub


'---------------------------------------------------------------------------
' Отрисовка проточки  типа II ( внутренняя резьба )
'---------------------------------------------------------------------------
SUB DrawProtocSec2(d_p, d_3, kx, ky) 

	Dim xp(2),yp(2)

	r_copy=r_norm
	
	Dim xt(2),yt(2)
	Dim xp1(2), yp1(2)

	ks=0

	while ks=0 

		ang1 = Math.ksAngle (0,0,-10*kx,10*ky)
		ang2 = Math.ksAngle (0,(d_3+d_p)*ky, -b_norm*kx, (d_3+d_p)*ky)

		math.ksCouplingLineLine 0,0,ang1,0,(d_3+d_p)*ky,ang2,r_copy,cn
		ks = Cn.GetCount	

		if (ky=-1) then
			if kx =1 then       
				index=3
			else 
				index=2
			end if
		else
			if kx =1 then       
				index=2
			else 
				index=3
			end if
		end if 

		xc=cn.GETxc(index)
		yc=cn.GETyc(index)

		x1=cn.GETx1(index)
		y1=cn.GETy1(index)

		x2=cn.GETx2(index)
		y2=cn.GETy2(index)

		IDA.ksClearArray
		math.ksIntersectLinSCir -b_norm*kx,10*ky,-b_norm*kx,-10*ky,xc,yc,r_copy,iDA
		ks = iDA.ksGetArrayCount

		If ks <> 0 Then 
			iDA.ksGetArrayItem 0, iPoint
			xp(0) = iPoint.x
			yp(0) = iPoint.y
			
			If ks > 1 Then 
				iDA.ksGetArrayItem 1, iPoint
				xp(1) = iPoint.x
				yp(1) = iPoint.y
			End If 
		End If   
		
		if ks=0 then 
			r_copy=r_copy+0.1
		end if

	Wend ' end while

	if  ((ks>1) and (yp(1)*ky > yp(0)*ky )) then
		yp(0) = yp(1)
	end if  

  	IDA.ksClearArray
	Math.ksTanLineAngCircle xc, yc, r_copy, 0, iDA
	ks = iDA.ksGetArrayCount
	
	If ks <> 0 Then   
		iDA.ksGetArrayItem 0, iPoint
		xt(0) = iPoint.x
		yt(0) = iPoint.y
		
		If ks > 1 Then
			iDA.ksGetArrayItem 1, iPoint
			xt(1) = iPoint.x
			yt(1) = iPoint.y
		End If 
	End If  
	line 1.5*b_norm*kx, 0, 0, 0, 1 

	iDA.ksClearArray
	f1=Math.ksAngle (xc,yc,x1,y1)
	f2=Math.ksAngle (xc,yc,xp(0),yp(0))
	f3=Math.ksAngle (1.5*b_norm*kx,d_p*ky,xc,d_p*ky)

	Math.ksIntersectArcLin xc,yc,r_copy,f1,f2,kx*ky,1.5*b_norm*kx,d_p*ky,f3,iDA
	kp = iDA.ksGetArrayCount
	
	If kp <> 0 Then      
		iDA.ksGetArrayItem 0, iPoint
		xp1(0) = iPoint.x
		yp1(0) = iPoint.y
		
		If kp > 1 Then      		    
			iDA.ksGetArrayItem 1, iPoint
		    xp1(1) = iPoint.x
		    yp1(1) = iPoint.y
		End If 
	End If 

	Doc.ksColouring &HFE000000
		Doc.ksBezier 0,0  
			Doc.kspoint -b_norm*kx,yp(0) - b_norm*0.3,11
			Doc.kspoint -b_norm*kx - b_norm*0.2,yp(0),11
			Doc.kspoint (-b_norm-1)*kx,b_norm*ky,11
			Doc.kspoint 0,b_norm*ky,11
			Doc.kspoint 1.5*b_norm*kx,d_p*ky,11
			Doc.kspoint 1.5*b_norm*kx,0,11
		Doc.ksEndObj
		Doc.ksBezier 0,0  
			Doc.kspoint 1.5*b_norm*kx,0,11
			Doc.kspoint 0,-b_norm*ky,11
			Doc.kspoint -b_norm*kx,yp(0) - b_norm * 0.3,11
		Doc.ksEndObj
	Doc.ksEndObj

	line 1.5*b_norm*kx,d_p*ky, xp1(1), d_p*ky, 2

	line  0, 0,x1,y1, 1 
	Doc.ksArcByPoint xc, yc, r_copy, x1, y1, xp(0), yp(0), ky * kx, 1
	line xp(0), yp(0), xp(0), yp(0) - b_norm*0.3, 1

	'++линии  обрыва++
	Doc.ksBezier 0,0  
		Doc.kspoint -b_norm*kx,yp(0) - b_norm*0.3,11
		Doc.kspoint -b_norm*kx - b_norm*0.2,yp(0),11
		Doc.kspoint (-b_norm-1)*kx,b_norm*ky,11
		Doc.kspoint 0,b_norm*ky,11
		Doc.kspoint 1.5*b_norm*kx,d_p*ky,11
		Doc.kspoint 1.5*b_norm*kx,0,11
	Doc.ksEndObj
	
	Doc.kshatch 0, hatchAngle, hatchStep, 0, 1, 0
		line 1.5*b_norm*kx, 0, 0, 0, 1 
		line 0, 0,x1,y1, 1
		Doc.ksArcByPoint xc,yc,r_copy,x1,y1,xp(0),yp(0),ky*kx,1
		line xp(0), yp(0), xp(0), yp(0) - b_norm*0.3, 1
		'++линии  обрыва++
		Doc.ksBezier 0,0  
			Doc.kspoint -b_norm*kx,yp(0) - b_norm*0.3,11
			Doc.kspoint -b_norm*kx - b_norm*0.2,yp(0),11
			Doc.kspoint (-b_norm-1)*kx,b_norm*ky,11
			Doc.kspoint 0,b_norm*ky,11
			Doc.kspoint 1.5*b_norm*kx,d_p*ky,11
			Doc.kspoint 1.5*b_norm*kx,0,11
		Doc.ksEndObj
	Doc.ksEndObj

	line 0,0,0,-b_norm*ky,1
	
	IF (Dims = true) THEN
	
		scale = GetScale
		
		ks = GetArrowType(b_norm)
		
		LinDimension - b_norm * kx, yp(0) * ky, 0, 0, 0, 10 + b_norm * 1.1 * scale, false, false, ks, ks, 0, 0, 0, 2
	 
		RadDimension xc, yc, r_norm, - 27, (90 + 15 * kx) * ky, -1, 1, 2
		
		IF kx=1 THEN
			ang1 = -45
			ang2 = 0
			nAng = -55
		ELSE
			ang1 = 180 + 45
			ang2 = 180
			nAng = 180 + 55
		END IF
		AngDimension 0, 0, 1.5 * b_norm * kx, 0, 0, 0,  ang1, ang2*ky, b_norm * 1.5 * scale + 5, 5, nAng

		LinDimension xc, yc + r_norm, xc, yc + r_norm - 10 - 10 / scale, - 10 - (b_norm * kx + b_norm * 0.2 + xc) * kx * scale * 1.5, 0, false, true, 1, 0, 1, d + delta_d, 0, 1
		
	END IF

	Doc.ksBezier 0,0  
		Doc.kspoint 1.5*b_norm*kx,0,11
		Doc.kspoint 0,-b_norm*ky,11
		Doc.kspoint -b_norm*kx,yp(0) - b_norm*0.3,11
	Doc.ksEndObj
END SUB

'---------------------------------------------------------------------------
' Отрисовка проточки типа 1 (метрическая ГОСТ 27148 наружная)
'---------------------------------------------------------------------------
SUB DrawProtocMetr2(d_p, d_3, kx, ky)
	IDA.ksClearArray
	angle1 = Math.ksAngle (0, 0, - (g2 - b_norm) * kx, - d_3 * ky)
	
	math.ksIntersectLinLin 0, 0, angle1, 0, - d_p * ky, 0, iDA
	ks = iDA.ksGetArrayCount
	If ks<>0 Then
		iDA.ksGetArrayItem 0, iPoint
	    xp = iPoint.x
	    yp = iPoint.y
	End If
	
	math.ksCouplingLineLine 0, 0, angle1, 0, - d_3 * ky, 180, r1_norm, cn
	ks = Cn.GetCount
	if (ky = 1) then
		if kx = 1 then
			index = 3
		else
			index = 2
		end if
	else
		if kx = 1 then
			index = 2
		else
			index = 3
		end if
	end if

	xc=cn.GETxc(index)
	yc=cn.GETyc(index)

	x1=cn.GETx1(index)
	y1=cn.GETy1(index)
	
	x2=cn.GETx2(index)
	y2=cn.GETy2(index)
	
	Xc1 = (- g2 + r_norm) * kx
	Yc1 = (- d_3 + r_norm) * ky
	x11 = xc1
	y11 = - d_3 * ky
	x21 = (- g2) * kx
	y21 = yc1

	doc.ksColouring &HFE000000
		Line 1.5 * b_norm * kx, 0, 0, 0, 1
		Line 0, 0, x1, y1, 1
		Doc.ksArcByPoint xc, yc, r1_norm, x1, y1, x2, y2, - ky * kx, 1
		Line x2, y2, x11, y11, 1
		Doc.ksArcByPoint xc1, yc1, r_norm, x11, y11, x21, y21, - ky * kx, 1
		Line x21, y21 + b_norm * 0.3, x21, - 1.3 * b_norm * ky, 1
		Doc.ksBezier 0, 0
			Doc.kspoint x21, y21 + b_norm * 0.3, 11
			Doc.kspoint - 1.3 * g2 * kx, - b_norm * 0.3 * ky, 11
			Doc.kspoint - 1.3 * g2 * kx, - b_norm * 0.8 * ky, 11
			Doc.kspoint x21, - 1.3 * b_norm * ky, 11
			Doc.kspoint - (g2 - b_norm) * kx, (- d_3 - 0.9 * b_norm) * ky, 11
			Doc.kspoint 0, - 1.2 * b_norm * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, - d_p * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, 0, 11
		Doc.ksEndObj
	Doc.ksEndObj

	Line 1.5 * b_norm * kx, 0, 0, 0, 1

	line 1.5 * b_norm * kx, - d_p * ky, xp * kx, - d_p * ky, 2
	line 0, 0, 0, - 1.2 * b_norm * ky, 1
	line - (g2 - b_norm) * kx, (- d_3 - b_norm * 0.1) * ky, - (g2 - b_norm) * kx, (- d_3 - 0.9 * b_norm) * ky, 2

	line 0, 0, x1, y1, 1

	Doc.ksArcByPoint xc, yc, r1_norm, x1, y1, x2, y2, -ky * kx, 1
	
	line x2, y2, x11, y11, 1
	Doc.ksArcByPoint xc1, yc1, r_norm, x11, y11, x21, y21, - ky * kx, 1

	Line x21, y21 + b_norm * 0.3, x21, - 1.3 * b_norm * ky, 1

	IF (Dims = true) THEN
		
		scale = getScale
		
		ks = GetArrowType(x21 + (g2 - b_norm) * kx)
		LinDimension x21, - 1.3 * b_norm * ky, - (g2 - b_norm) * kx, (- d_3 - 0.9 * b_norm) * ky, 0, - 20, false, false, ks, ks, 0, 0, 0, 1
		
		ks = GetArrowType(x21)
		LinDimension x21, - 1.3 * b_norm * ky, 0, - 1.2 * b_norm * ky, 0, - 35, false, false, ks, ks, 0, 0, 0, 1
	 
		IF r1_norm = r_norm THEN

			b = - 5 - 10 / scale
			a = - abs(xc1 - xc) * 0.5
			alfa = atn(b / a) * 180 / pi
			c = b / sin(alfa * pi / 180)
			xx = c
			
			IF (xx < -15) THEN xx = -15
			iDA.ksClearArray
			iPoint.init
			iPoint.x = xc1
			iPoint.y = yc1
			iDA.ksAddArrayItem -1, iPoint
			iPoint.init
			iPoint.x = xc
			iPoint.y = yc
			iDA.ksAddArrayItem -1, iPoint

			' #VBG
			VBGuru.BranchingRadialDimension kompas, iDA, r_norm, xx * scale, ((alfa - 180) * kx) * ky, -1, 0, x0, y0, 1, 0, 0
		ELSE
			xx = -(((abs(xc1 - xc) * 0.5) / (sin((pi * 5) / 180))) * 0.9) * scale
			
			IF (xx < -25) THEN xx = -25
			
			RadDimension xc, yc, r1_norm, xx, -(90 - 5 * kx) * ky, 1, 1, 2
			RadDimension xc1, yc1, r_norm, xx, -(90 + 5 * kx) * ky, -1, 1, 2
		END IF

		IF kx=1 THEN
			ang1 = 0
			ang2 = angle1 - 180
			nAng = 55
		ELSE
			ang1 = 180
			ang2 = 180 - (angle1 - 180)
			nAng = 180 - 55
		END IF
		AngDimension 0, 0, 1.5 * b_norm * kx, 0, 0, 0, ang1, ang2 * ky, 15, 8, nAng
	
		xx = - 1.3 * g2 * kx
		
		LinDimension x11, y2, 0, - 10 - 10 / scale, - 20 + xx * kx * scale, 0, false, true, 1, 0, 1, d - 2 * d_3, 0, 2
	END IF

	Doc.ksBezier 0, 0
		Doc.kspoint x21, y21 + b_norm * 0.3, 11
		Doc.kspoint - 1.3 * g2 * kx, - b_norm * 0.3 * ky, 11
		Doc.kspoint - 1.3 * g2 * kx, - b_norm * 0.8 * ky, 11
		Doc.kspoint x21, - 1.3 * b_norm * ky, 11
		Doc.kspoint - (g2 - b_norm) * kx, (- d_3 - 0.9 * b_norm) * ky, 11
		Doc.kspoint 0, - 1.2 * b_norm * ky, 11
		Doc.kspoint 1.5 * b_norm * kx, - d_p * ky, 11
		Doc.kspoint 1.5 * b_norm * kx, 0, 11
	Doc.ksEndObj
END SUB

'---------------------------------------------------------------------------
' Отрисовка проточки типа I (метрическая ГОСТ 27148 внутренняя)
'---------------------------------------------------------------------------
SUB DrawProtocSecMetr2(d_p, d_3, kx, ky) 
	angle1 = Math.ksAngle (0, 0, - (g2 - b_norm) * kx, (d_3 + d_p) * ky)
	ang2 = Math.ksAngle (0, (d_3 + d_p) * ky, - b_norm * kx, (d_3 + d_p) * ky)

	math.ksCouplingLineLine 0, 0, angle1, 0, (d_3 + d_p) * ky, ang2, r1_norm, cn
	ks = Cn.GetCount

	if (ky=-1) then
		if kx =1 then       
			index=3
		else 
			index=2
		end if
	else
		if kx =1 then       
			index=2
		else 
			index=3
		end if
	end if 

	xc=cn.GETxc(index)
	yc=cn.GETyc(index)

	x1=cn.GETx1(index)
	y1=cn.GETy1(index)

	x2=cn.GETx2(index)
	y2=cn.GETy2(index)

	xc1=(-g2+r_norm)*kx
	yc1=(d_3+d_p-r_norm)*ky
	x11=xc1 
	y11=(d_3+d_p)*ky
	x21=(-g2)*kx
	y21=yc1

	Doc.ksColouring &HFE000000
		Doc.ksBezier 0, 0
			Doc.kspoint (- g2) * kx, y21 - b_norm * 0.3, 11
			Doc.kspoint (- g2) * kx - b_norm * 0.1, y21, 11

			Doc.kspoint (- g2 - 1) * kx, 1.1 * b_norm * ky, 11
			Doc.kspoint (- (g2 - b_norm)) * kx, 1.1 * b_norm * ky, 11
			Doc.kspoint 0, 1.1 * b_norm * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, d_p * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, 0, 11
		Doc.ksEndObj

		Doc.ksBezier 0, 0
			Doc.kspoint 1.5 * b_norm * kx, 0, 11
			Doc.kspoint 0, - b_norm * 1.1 * ky, 11
			Doc.kspoint x21 + b_norm * kx, - b_norm * 1.1 * ky, 11 
			Doc.kspoint (- g2) * kx, y21 - b_norm * 0.3, 11
		Doc.ksEndObj
	Doc.ksEndObj

	line 1.5 * b_norm * kx, 0, 0, 0, 1
	GetIntersectLinSLin 1.5 * b_norm * kx, d_p * ky, - g2 * kx, d_p * ky, 0, 0, angle1, xpe, ype
	line 1.5 * b_norm * kx, d_p * ky, xpe, ype, 2
	line 0, 0, x1, y1, 1

	Doc.ksArcByPoint xc,yc,r1_norm,x1,y1,x2,y2,ky*kx,1

	line x2,y2,x11,y11,1
	Doc.ksArcByPoint xc1,yc1,r_norm,x11,y11,x21,y21,ky*kx,1
	
	line x21,y21,x21,y21-b_norm*0.3,1
	'++линии  обрыва++
	Doc.ksBezier 0,0
		Doc.kspoint (- g2) * kx, y21 - b_norm * 0.3, 11
		Doc.kspoint (- g2) * kx - b_norm * 0.1, y21, 11

		Doc.kspoint (- g2 - 1) * kx, 1.1 * b_norm * ky, 11
		Doc.kspoint (- (g2 - b_norm)) * kx, 1.1 * b_norm * ky, 11
		Doc.kspoint 0, 1.1 * b_norm * ky, 11
		Doc.kspoint 1.5 * b_norm * kx, d_p * ky, 11
		Doc.kspoint 1.5 * b_norm * kx, 0, 11
	Doc.ksEndObj

	Doc.kshatch 0, hatchAngle, hatchStep, 0, 1, 0
		line 1.5*b_norm*kx,0,0,0,1
		line 0,0,x1,y1,1
		
		Doc.ksArcByPoint xc,yc,r1_norm,x1,y1,x2,y2,ky*kx, 1 
		
		line x2,y2,x11,y11,1
		Doc.ksArcByPoint xc1,yc1,r_norm,x11,y11,x21,y21,ky*kx, 1 
		
		line x21,y21,x21,y21-b_norm*0.3,1
		'++линии  обры   ва++   
		Doc.ksBezier 0,0
			Doc.kspoint (- g2) * kx, y21 - b_norm * 0.3, 11
			Doc.kspoint (- g2) * kx - b_norm * 0.1, y21, 11

			Doc.kspoint (- g2 - 1) * kx, 1.1 * b_norm * ky, 11
			Doc.kspoint (- (g2 - b_norm)) * kx, 1.1 * b_norm * ky, 11
			Doc.kspoint 0, 1.1 * b_norm * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, d_p * ky, 11
			Doc.kspoint 1.5 * b_norm * kx, 0, 11
		Doc.ksEndObj
	Doc.ksEndObj
	line 0, 0, 0, -b_norm * 1.1 * ky, 1

	IF (Dims = true) THEN
		scale = getScale
		
		ks = GetArrowType(- b_norm - (g2 - b_norm) - (x21 + b_norm))
		
		LinDimension (-b_norm - (g2 - b_norm)) * kx, y21, x21 + b_norm * kx, y2 - d_p * 0.5 * ky, 0, 5 + 1.2 * b_norm * scale, false, false, ks, ks, 0, 0, 0, 1
		
		
		ks = GetArrowType(- b_norm - (g2 - b_norm))
		
		LinDimension (- b_norm - (g2 - b_norm)) * kx, y21, 0, 0, 0, 20 + 1.2 * b_norm * scale, false, false, ks, ks, 0, 0, 0, 1
	 
	 
 		IF r1_norm = r_norm THEN

			b = - (b_norm * 1.1 - yc) - 10 / scale
			
			if (b > - 16 / scale) then b = - 16 / scale
			
			a = - abs(xc1 - xc) * 0.5
			alfa = atn(b / a) * 180 / pi
			c = b / sin(alfa * pi / 180)
			xx = c

			iDA.ksClearArray
			iPoint.init
			iPoint.x = xc1
			iPoint.y = yc1
			iDA.ksAddArrayItem -1, iPoint
			iPoint.init
			iPoint.x = xc
			iPoint.y = yc
			iDA.ksAddArrayItem -1, iPoint

			' #VBG
			VBGuru.BranchingRadialDimension kompas, iDA, r_norm, (xx - r_norm) * scale, 180 - (alfa * kx) * ky, 1, 0, x0, y0, 1, 0, 0
			
		ELSE
			xx1 = - b_norm * 1.5 * scale - 10
			xx = ((-abs(xc1 - xc) * 0.5 / sin(pi * 5 / 180)) * 0.9) * scale
			if (xx < xx1) THEN xx = xx1

			RadDimension xc, yc, r1_norm, xx, (90 - 5 * kx) * ky, 1, 1, 2
			RadDimension xc1, yc1, r_norm, xx, (90 + 5 * kx) * ky, -1, 1, 2
		END IF
		
		IF kx = 1 THEN
			ang1 = -(180 - angle1)
			ang2 = 0
			nAng = -55
		ELSE
			ang1 = 180 - (180 - angle1)
			ang2 = 180
			nAng = 180 + 55
		END IF
		
		AngDimension 0, 0, 1.5 * b_norm * kx, 0, 0, 0,  ang1, ang2 * ky, 1.5 * b_norm + 5, 10, nAng
	
		xx = b_norm + 0.05
		IF xx < 18 / scale THEN xx = 18 / scale
		dimWidth = - 1.5 * b_norm
		if dimWidth > - 20 / scale then dimWidth = - 20 / scale
		LinDimension x11, y2, x11, dimWidth, -xx * kx * scale, 0, false, true, 1, 0, 1, d + d_3 * 2, 0, 1
	END IF  

	Doc.ksBezier 0, 0
		Doc.kspoint 1.5 * b_norm * kx, 0, 11
		Doc.kspoint 0, - b_norm * 1.1 * ky, 11
		Doc.kspoint x21 + b_norm * kx, - b_norm * 1.1 * ky, 11 
		Doc.kspoint (- g2) * kx, y21 - b_norm * 0.3, 11
	Doc.ksEndObj

	line x21 + b_norm * kx, -b_norm * 1.1 * ky, x21 + b_norm * kx, y2 - d_p * 0.5 * ky, 2
END Sub

'++++++ 
SUB Init()
	SELECT CASE TYPE_Protoc
	
	CASE ProtocMetr:
		SELECT CASE  TYPE_LEVEL 
		
		CASE OUTSIDE_TYPE1 :
			P = 6' резьба
			b_norm =  12   'ширина проточки
			r_norm =  3 'радиус 
			r1_norm = 1
			Delta_d = 9' 
			c = 4
			
        CASE OUTSIDE_TYPE2 :
			P = 6' резьба
			b_norm =  16   'ширина проточки
			r_norm =  8.5 'радиус 
			r1_norm = 0
			Delta_d = 9' 
			c = 8

        CASE INSIDE_TYPE1 :
			P = 6' резьба
			b_norm =  16   'ширина проточки
			r_norm =  3 'радиус 
			r1_norm = 1
			Delta_d = 2' 
			c = 4

        CASE INSIDE_TYPE2 :
			P = 6' резьба
			b_norm =  18.9   'ширина проточки
			r_norm =  10.5 'радиус 
			r1_norm = 0
			Delta_d = 2' 
			c = 10.5

	  End SELECT

	CASE ProtocTrap:
		'трапец.
		SELECT CASE  TYPE_LEVEL 
      
		CASE INSIDE_TYPE1 :
			' внутренняя
			b_norm  = 8'   //ширина поточки
			r_norm  = 2'   //
			r1_norm = 1'
			delta_d = 1'
			c       = 1.6'
			p       = 2'  /* шаг резьбы */
			d       = 20'  /*диаметр резьбы*/

		CASE OUTSIDE_TYPE1 :
			b_norm  = 3'   //ширина поточки
			r_norm  = 1'   //
			r1_norm = 0.5'
			delta_d = 3'
			c       = 1.6'
			p       = 2'  /* шаг резьбы */
			d       = 20'  /*диаметр резьбы*/

		END SELECT       

	CASE ProtocTrub:
		'трубная
		SELECT CASE  TYPE_LEVEL 
		CASE INSIDE_TYPE1:
		
			If  FlagTrub =0 Then 
				'цилиндрич
				' внутренняя
				b_norm  = 10'   //ширина поточки
				r_norm  = 3'   //
				r1_norm = 1'
				delta_d = 165.0'
				c       = 1.6'
				p       = 2.309'  /* шаг резьбы */
				d       = 163.83'  /*диаметр резьбы*/
			End If
			
			If  FlagTrub =1 Then 
				'коническая  
				' внутренняя
				b_norm  = 8'   //ширина поточки
				r_norm  = 2'   //
				r1_norm = 1'
				delta_d = 165.0'
				c       = 2'
				p       = 2.309'  /* шаг резьбы */
				d       = 163.83'  /*диаметр резьбы*/
			End If
			
			If  FlagTrub =2 Then 
				'дюймовая  
				' внутренняя
				b_norm  = 7'   //ширина поточки
				r_norm  = 1.6'   //
				r1_norm = 1'
				delta_d = 60.5'
				c       = 2'
				p       = 2.309'  /* шаг резьбы */
				d       = 60.092'  /*диаметр резьбы*/
			End If
			
		CASE OUTSIDE_TYPE1:
		
			If  FlagTrub =0 Then 
				'цилиндрич
				b_norm  = 6'   //ширина поточки
				r_norm  = 1.6'   //
				r1_norm = 1'
				delta_d = 160'
				c       = 2.5'
				p       = 2.309'  /* шаг резьбы */
				d       = 163.83' /*диаметр резьбы*/
			End If

			If  FlagTrub =1 Then 
				'коническая
				b_norm  = 5'   //ширина поточки
				r_norm  = 1.6'   //
				r1_norm = 0.5'
				delta_d = 29.5'
				c       = 2'
				p       = 2.309'  /* шаг резьбы */
				d       = 33.25' /*диаметр резьбы*/
			End If
			
			If  FlagTrub= 2 Then 
				b_norm  = 5'   //ширина поточки
				r_norm  = 1.5'   //
				r1_norm = 0.5'
				delta_d = 55.0'
				c       = 2'
				p       = 2.309'  /* шаг резьбы */
				d       = 60.092'  /*диаметр резьбы*/
			End If
		END SELECT
	END SELECT
END SUB